-- UISkillUpgradeSub
-- Created by chengb Mar/31/2015
-- 终极职业升级子界面

UISkillUpgradeSub = class("UISkillUpgradeSub", function()
    return cc.Layer:create();
end);

function UISkillUpgradeSub.create(careerId, featureId)
    return UISkillUpgradeSub.new(careerId, featureId);
end

-- 内部函数声明
local resize;

function UISkillUpgradeSub:ctor(careerId, featureId)
    -- 初始化
    self:setName("UISkillUpgradeSub");
    local node = cc.CSLoader:createNode("layout/hero/SkillUpgradeSub.csb");
    self:addChild(node);
    self.node = node;

    self.careerId = careerId;

    local pet = ME.user:getActivePet();
    self.pet = pet;

    self.featureId = featureId;

    -- 需要显示在头部菜单上面
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UISkillUpgradeSub:redraw()
    -- 重绘技能框
    self:redrawSkillInfo();

    -- 重绘返回重绘按钮
    local btnBack = findChildByName(self.node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 重绘强化按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_intensify"));

    -- 显示当前探索点
    local spNode = findChildByName(self.node, "BT/sp_node");
    local iconImg = findChildByName(spNode, "icon");
    local icon = FieldsM.query("search_point", "icon");
    iconImg:loadTexture(getSmallAttribIconPath(icon));
    local amountLabel = findChildByName(spNode, "amount");
    TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true, -2);
    amountLabel:setString(tonumber(ME.user:queryAttrib("search_point")));
end

-- 获取基础属性值
function UISkillUpgradeSub:getBasePropValue(baseProps, field)
    for _, arr in pairs(baseProps) do
        if arr[2] == field then
            return arr[3];
        end
    end
    return 0;
end

-- 重绘技能详细信息
function UISkillUpgradeSub:redrawSkillInfo()
    -- 显示名称
    local featureInfo = CareerM.getFeatureInfo(self.pet, self.careerId, self.featureId);

    local skillId = featureInfo["skill_id"];
    local skillInfo = SkillM.query(skillId);
    local isNewSkill = iif(featureInfo["is_new_skill"] == 1, true, false);

    -- 显示技能名称
    local nameLabel = findChildByName(self.node, "CT/name");
    local nameStr = "";
    nameStr = skillInfo["name"] .. " " .. covertNumToRoman(skillInfo["level"]);
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(nameStr);

    -- 显示基础属性
    local baseProps = SkillM.getSkillBaseProps(skillId);

    local content = "";
    local len = #baseProps
    if len > 0 then
        for i, arr in pairs(baseProps) do
            local field = arr[2];
            local value = arr[3];
            local fieldName = FieldsM.getFieldName(field, "name");
            content = content .. fieldName .. "[blue]" .. "+" .. value .. "[-]";
            if i < len then
                content = content .. getLocStr("pause_mark");
            end
        end
    end

    content = content .. "\n\n";

    -- 显示其他属性描述
    local propLabel = findChildByName(self.node, "CT/prop_desc");
    local propStr = SkillM.getSkillDesc(skillId, true);

    content = content .. propStr;
    TextStyleM.setTextStyle(propLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(propLabel, content);

    -- 显示探索点消耗
    local costInfo = CareerM.calcUpgradeCost(self.pet.classId, self.careerId);
    local needSp = costInfo["search_point"];
    local costStr = string.format(getLocStr("cost_sp_format2"), needSp);
    local costLabel = findChildByName(self.node, "CT/cost_node/cost_label");
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(costLabel, costStr);

    -- 显示背景称号图标
    -- 放放、半透明显示
    local bgIconImg = findChildByName(self.node, "CT/bg_icon");
    local careerInfo = CareerM.getCareerInfo(self.careerId);
    bgIconImg:loadTexture(getCareerIconPath(careerInfo["icon"]));
    bgIconImg:setScale(1.8);
    bgIconImg:setOpacity(50);
end

-- 注册点击事件
function UISkillUpgradeSub:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISkillUpgradeSub");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册学习按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_receive");
            local featureId = self.featureId;
            -- 判断能否升级
            local ret = CareerM.canUpgrade(self.pet.classId, self.careerId, featureId, true);
            if ret ~= true then
                if type(ret) == "string" then
                    alert(ret);
                end
                return false;
            end

            -- 升级特性
            DungeonActionM.go("upgrade_career", self.careerId, featureId);
        end
    end
    btnOk:addTouchEventListener(onOkClick);
end

-- 注册事件处理回调函数
function UISkillUpgradeSub:registerEventCallback()
    EventMgr.register("UISkillUpgradeSub", event.UPGRADE_CAREER, function(args)
        local careerId  = args.careerId;
        local level     = args.level;
        local featureId = args.feature;
        local ret = args.ret;

        if featureId ~= self.featureId then
            -- 不是特性升级，不处理
            return;
        end

        if featureId == 4 then
            local curArtifact = ArtifactM.getCarryArtifact();
            local maxLevel = FormulaM.invoke("CALC_CAREER_STRENGTHEN_LEVEL", curArtifact);
            local artiFeature = CareerM.getFeatureInfo(self.pet, self.careerId, 4);
            local curLevel = artiFeature["cur_level"];
            if curLevel + 1 > maxLevel then
                -- 关闭界面
                if ret ~= nil and #ret > 0 then
                    showBonusNotifyForm(getLocStr("upgrade_career_bonus"), ret);
                end

                UIMgr.getCurrentScene():removeFormByName("UISkillUpgradeSub");
                return;
            end
        end

        -- 特性已满级
        if CareerM.isFeatureMaxLevel(self.pet, self.careerId, featureId) then
            if ret ~= nil and #ret > 0 then
                showBonusNotifyForm(getLocStr("upgrade_career_bonus"), ret);
            end
            -- 关闭界面
            UIMgr.getCurrentScene():removeFormByName("UISkillUpgradeSub");
        else
            if ret ~= nil and #ret > 0 then
                showBonusNotifyForm(getLocStr("upgrade_career_bonus"), ret, function() self:redraw() end);
            end
            self:redraw();
        end

    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            EventMgr.removeAll("UISkillUpgradeSub");
        end
    end);
end

